package com.huawei.struts.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.upload.FormFile;

public class FileUploadUtil {
	
	public static String getFileName(HttpServletRequest request,FormFile file) throws IOException{
		//目标文件夹
		String diskDir="/upload/attachment1";
		File uploadFilePath=new File(diskDir);
		if(!uploadFilePath.exists()){
			uploadFilePath.mkdir();
		}
		 
		if(file.getFileSize()>0){
		//String dir=request.getSession(true).getServletContext().getRealPath(diskDir);
		SimpleDateFormat dateformat1=new SimpleDateFormat("yyyyMMddHHmmssSSS");
		String date=dateformat1.format(new Date());
		String fileName=request.getSession().getId()+"_"+date+"_"+file.getFileName();
		OutputStream fos=null;
        fos=new FileOutputStream(diskDir+"/"+fileName);   
        fos.write(file.getFileData(),0,file.getFileSize());   
        fos.flush();   
        fos.close();  
        return diskDir+"/"+fileName;
		}
		else{
			return "";
		}
		
	}

}
